/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulator.network;

import java.util.ArrayList;
import java.sql.*;

/**
 *
 * @author Pascal Schwenke <pascal.schwenke@gmail.com>
 */
public class Network {

    ArrayList<Cell> cells;
    public Network() {
        cells = new ArrayList<>();
    }

    //Returns a deep copy of this networks without users.
    public Network EmptyNetwork() {
        Network n = new Network();
        for (int i = 0; i < cells.size(); i++) {
            n.cells.add(new Cell(cells.get(i).id, cells.get(i).type, cells.get(i).longitude, cells.get(i).latitude, 0, cells.get(i).neighbors));
        }
        return n;
    }

    public Cell GetCellById(int id) {
        for (int i = 0; i < cells.size(); i++) {
            if(cells.get(i).id==id)
                return cells.get(i);
        }
        //TODO: improve this!
        return null;
    }

    public void InitializeFromDB(ResultSet resultSet) throws Exception {
        /*
         String query = "SELECT * FROM " + tableName;
         Connection connect;
         Statement statement;
         ResultSet resultSet;

         // This will load the MySQL driver, each DB has its own driver
         Class.forName("com.mysql.jdbc.Driver");
         // Setup the connection with the DB
         connect = DriverManager
         .getConnection(url, user, password);
         statement = connect.createStatement();
         resultSet = statement.executeQuery(query);
         */
        //Create network from retrieved data
        while (resultSet.next()) {
            Cell c = new Cell(resultSet.getInt("id"), resultSet.getString("type"), resultSet.getInt("longitude"), resultSet.getInt("latitude"), resultSet.getInt("userCount"));
            c.neighbors = resultSet.getString("neighbors").split(",");
            cells.add(c);
        }

    }
}
